package org.thirdHero.db.service;

import com.github.pagehelper.PageHelper;
import org.thirdHero.db.dao.SysDeptMapper;
import org.thirdHero.db.domain.SysDept;
import org.thirdHero.db.domain.SysDeptExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.util.List;

@Service
public class SysDeptService {
    private static final Logger logger = LoggerFactory.getLogger(SysDeptService.class);
    @Resource
    private SysDeptMapper sysDeptMapper;
    private final SysDept.Column[] result = new SysDept.Column[]{
        SysDept.Column.id, SysDept.Column.num, SysDept.Column.pid, SysDept.Column.pids, SysDept.Column.simplename, SysDept.Column.fullname, SysDept.Column.tips, SysDept.Column.version, SysDept.Column.level
    };

    /**
     * 根据部门简称查询部门信息
     * @param simpleName
     * @return
     */
    public SysDept findDeptBySimpleName(String simpleName){
        SysDeptExample sysDeptExample = new SysDeptExample();
        SysDeptExample.Criteria criteria = sysDeptExample.createCriteria();
        criteria.andSimplenameEqualTo(simpleName);
        return sysDeptMapper.selectOneByExample(sysDeptExample);
    }

    public List<SysDept> querySelective(String simplename, Integer page, Integer limit, String sort, String order){
        SysDeptExample example = commonSelective(simplename);
        PageHelper.startPage(page,limit);
        return sysDeptMapper.selectByExampleSelective(example,result);
    }
    public int countSelective(String name,Integer page,Integer limit,String sort,String order){
        SysDeptExample example = commonSelective(name);
        return (int) sysDeptMapper.countByExample(example);
    }
    private SysDeptExample commonSelective(String name) {
        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andSimplenameLike("%"+name+"%");
        }
        return example;
    }
    public List<SysDept> findLiteMallDeptByPid(Integer pid){
        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(pid);
        return sysDeptMapper.selectByExample(example);
    }
    public SysDept findLIteMallDeptById(Integer id){
        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        return sysDeptMapper.selectOneByExample(example);
    }
    public List<SysDept> queryLiteMallDeptBySimpleName(String simpleName){
        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        criteria.andSimplenameEqualTo(simpleName);
        return sysDeptMapper.selectByExample(example);
    }

    public void insertLiteMallDept(SysDept sysDept) {
        this.sysDeptMapper.insert(sysDept);
    }

    public int deleteDeptById(Integer id) {
        return sysDeptMapper.deleteByPrimaryKey(id);
    }

    public int updateLIteDeptByid(SysDept sysDept) {
        return sysDeptMapper.updateByPrimaryKey(sysDept);
    }
}
